package app.itgungnir.compose.wan.business.my_website

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import app.itgungnir.compose.wan.widgets.NoMoreItem
import app.itgungnir.compose.wan.widgets.WanDialog
import app.itgungnir.compose.wan.widgets.WanFab
import app.itgungnir.compose.wan.widgets.WanScaffold
import com.google.accompanist.insets.navigationBarsPadding

@Composable
fun MyWebsiteScreen(navController: NavController = rememberNavController()) {
    val viewModel = viewModel<MyWebsiteViewModel>()
    // UI
    val editDialogState = remember { mutableStateOf(false) }
    val websiteState = remember { mutableStateOf<MyWebsite?>(null) }
    WanScaffold(
        modifier = Modifier.navigationBarsPadding(),
        navController = navController,
        title = "我常用的网站",
        loadingState = viewModel.loadingState.collectAsState(),
        loadingJob = viewModel.loadingJob,
        errorState = viewModel.errorState.collectAsState(initial = null),
        floatingActionButton = {
            WanFab(icon = Icons.Filled.Add) {
                viewModel.dismissDialogState.value = false
                websiteState.value = null
                editDialogState.value = true
            }
        }
    ) {
        val pagingItems by viewModel.myWebsiteFlow.collectAsState(initial = null)
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colors.surface)
        ) {
            pagingItems?.let { list ->
                items(list) { website ->
                    MyWebsiteItem(
                        navController = navController,
                        item = website,
                        editCallback = {
                            viewModel.dismissDialogState.value = false
                            websiteState.value = it
                            editDialogState.value = true
                        },
                        deleteCallback = {
                            viewModel.deleteWebsite(it)
                        }
                    )
                }
            }
            item {
                NoMoreItem()
            }
        }
    }
    WebsiteEditDialog(state = editDialogState, websiteState = websiteState, viewModel = viewModel)
}

@Composable
private fun WebsiteEditDialog(
    state: State<Boolean>,
    websiteState: State<MyWebsite?>,
    viewModel: MyWebsiteViewModel = viewModel()
) {
    WanDialog(
        state = state,
        loadingJob = viewModel.dialogLoadingJob,
        dismissState = viewModel.dismissDialogState.collectAsState(),
        errorState = viewModel.dialogErrorState.collectAsState(initial = null),
        paddingValues = PaddingValues(horizontal = 20.dp, vertical = 10.dp)
    ) {
        val originWebsite = websiteState.value
        WebsiteEditDialogUI(
            origin = originWebsite,
            confirmCallback = { name, link ->
                when (originWebsite == null) {
                    true -> viewModel.addWebsite(name, link)
                    else -> viewModel.updateWebsite(originWebsite.copy(name = name, link = link))
                }
            },
            loadingState = viewModel.dialogLoadingState.collectAsState(),
            errorState = viewModel.dialogErrorState.collectAsState(initial = null)
        )
    }
}
